<?php
class ControllerAddonBds extends Controller
{
	private $error = array();
	private $sitemapid;
	public function __construct() 
	{
		$this->data['permissionAccess'] = true;
		$this->data['permissionAdd'] = true;
		$this->data['permissionEdit'] = true;
		$this->data['permissionDelete'] = true;
		
		$this->sitemapid = $this->request->get['sitemapid'];
		
		if(!$this->user->hasPermission($this->sitemapid, "access"))
		{
			$this->data['permissionAccess'] = false;
		}
		if(!$this->user->hasPermission($this->sitemapid, "add"))
		{
			$this->data['permissionAdd'] = false;
		}
		if(!$this->user->hasPermission($this->sitemapid, "edit"))
		{
			$this->data['permissionEdit'] = false;
		}
		if(!$this->user->hasPermission($this->sitemapid, "delete"))
		{
			$this->data['permissionDelete'] = false;
		}
		
		$this->load->model("addon/bds");
		$this->load->model("addon/nhom");
		$this->load->model("core/user");
		$this->load->helper('image');
		$this->data['nhucau'] = array();
		$this->model_addon_nhom->getTree("nhucau",$this->data['nhucau']);
		unset($this->data['nhucau'][0]);
		
		$this->data['loaibds'] = array();
		$this->model_addon_nhom->getTree("loaibds",$this->data['loaibds']);
		unset($this->data['loaibds'][0]);
		
		$this->data['huong'] = array();
		$this->model_addon_nhom->getTree("huong",$this->data['huong']);
		unset($this->data['huong'][0]);
		
		$this->data['khuvuc'] = array();
		$this->model_addon_nhom->getTree("khuvuc",$this->data['khuvuc']);
		unset($this->data['khuvuc'][0]);
		
		$where = " AND 	usertypeid <> 'member' ";
		$this->data['users'] = $this->model_core_user->getList($where);
		
	}
	
	public function index()
	{
		
		if($this->data['permissionAccess']==false)
			$this->response->redirect("?route=common/permission");
		
		$this->document->title = $this->language->get('heading_title');
		
		$this->load->model("addon/bds");
		$this->getList();
	}
	
	public function insert()
	{
		if(!$this->user->hasPermission($this->sitemapid, "add"))
		{
			$this->response->redirect("?route=common/permission");
		}
		
    	$this->getForm();
	}
	
	public function update()
	{
		if(!$this->user->hasPermission($this->sitemapid, "edit"))
		{
			$this->response->redirect("?route=common/permission");
		}
		else
		{
			//$this->load->language('addon/bds');
			//$this->data = array_merge($this->data, $this->language->getData());
			
			
			$this->load->model("addon/bds");
			$this->data['readonly'] = 'readonly="readonly"';
			
		
			$this->getForm();
		}
		
  	}
	
	
	public function delete() 
	{
		$listbdsid=$this->request->post['delete'];
		//$listbdsid=$_POST['delete'];
		$this->load->model("addon/bds");
		if(count($listbdsid))
		{
			foreach($listbdsid as $bdsid)
				$this->model_addon_bds->delete($bdsid);
			$this->data['output'] = "Xóa thành công";
		}
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}
	
	private function getList() 
	{
		$this->data['insert'] = $this->url->http('addon/bds/insert&sitemapid='.$this->sitemapid);
		$this->data['delete'] = $this->url->http('addon/bds/delete');		
		
		$where = "";
		$datasearchlike['bdsid'] = $this->request->get['bdsid'];
		$datasearchlike['tieude'] = $this->request->get['tieude'];
		$datasearch['nhucau'] = $this->request->get['nhucau'];
		$datasearch['loaibds'] = $this->request->get['loaibds'];
		$datasearch['huong'] = $this->request->get['huong'];
		$datasearch['khuvuc'] = $this->request->get['khuvuc'];
		
		$arr = array();
		foreach($datasearchlike as $key => $item)
		{
			if($item !="")
				$arr[] = " AND " . $key ." like '%".$item."%'";
		}
		
		foreach($datasearch as $key => $item)
		{
			if($item !="")
				$arr[] = " AND " . $key ." = '".$item."'";
		}
		
		if($this->request->get['giatu'])
		{
			$arr[] = " AND gia >= '".$this->string->toNumber($this->request->get['giatu'])."'";
		}
		
		if($this->request->get['giaden'])
		{
			$arr[] = " AND gia <= '".$this->string->toNumber($this->request->get['giaden'])."'";
		}
		
		$where = implode("",$arr);
		
		if($this->user->getUserTypeId()!="admin")
		{
			$where.=" AND assignto = '".$this->user->getId()."'";	
		}
		
		$where.=" ORDER BY `dlbds`.`statusdate` DESC";
		
		$this->data['datas'] = array();
		$rows = $this->model_addon_bds->getList($where);
		//Page
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		//for($i=0; $i <= count($this->data['datas'])-1 ; $i++)
		{
			$this->data['datas'][$i] = $rows[$i];
			$this->data['datas'][$i]['imagethumbnail'] = HelperImage::resizePNG($this->data['datas'][$i]['imagepath'], 100, 100);
			$this->data['datas'][$i]['link_edit'] = $this->url->http('addon/bds/update&sitemapid='.$this->sitemapid.'&bdsid='.$this->data['datas'][$i]['bdsid']);
			$this->data['datas'][$i]['text_edit'] = "Edit";
			
			
		}
		$this->data['refres']=$_SERVER['QUERY_STRING'];
		$this->id='content';
		$this->template="addon/bds_list.tpl";
		$this->layout="layout/center";
		
		$this->render();
	}
	
	
	private function getForm()
	{
		
		
		$this->data['DIR_UPLOADPHOTO'] = HTTP_SERVER."index.php?route=common/uploadpreview";
		$this->data['DIR_UPLOADATTACHMENT'] = HTTP_SERVER."index.php?route=common/uploadattachment";

		$where = " AND bdsid <> '".$this->request->get['bdsid']."'";
		$this->data['listdonvitinh'] = $this->model_addon_bds->getList($where);
		
		if ((isset($this->request->get['bdsid'])) ) 
		{
      		$this->data['item'] = $this->model_addon_bds->getItem($this->request->get['bdsid']);
			$this->data['item']['imagethumbnail'] = HelperImage::resizePNG($this->data['item']['imagepath'], 200, 200);
			$listfile = $this->data['item']['attachment'];
			$listfileid=array();
			if($listfile != "")
				$listfileid=split(",",$listfile);
			$this->data['attachment']=array();
			foreach($listfileid as $key => $item)
			{
				$file = $this->model_core_file->getFile($item);
				if(count($file))
				{
					$this->data['attachment'][$key] = $file;
					$this->data['attachment'][$key]['imagethumbnail'] = HelperImage::resizePNG($this->data['attachment'][$key]['filepath'], 50, 50);
					if(!$this->string->isImage($this->data['attachment'][$key]['extension']))
						$this->data['attachment'][$key]['imagethumbnail'] = DIR_IMAGE."icon/dinhkem.png";
				}
			}
    	}
		else
		{
			$this->data['item']['assignto'] = $this->user->getId();
		}
		
		$this->id='content';
		$this->template='addon/bds_form.tpl';
		$this->layout="layout/center";
		
		$this->render();
	}
	
	public function save()
	{
		$data = $this->request->post;
		if($this->validateForm($data))
		{
			$this->load->model("addon/bds");
			$item = $this->model_addon_bds->getItem($data['bdsid']);
			
			$listAttachment=$data['attimageid'];
			if(count($listAttachment))
			{
				$this->model_core_file->updateListFileTemp($listAttachment);
				
				$list = implode(",",$listAttachment);
				$data['attachment'] = $list;
			}
			$listdelfile=$data['delfile'];
			if(count($listdelfile))
				foreach($listdelfile as $item)
					$this->model_core_file->deleteFile($item);
			
			
			if(count($item)==0)
			{
				$this->model_addon_bds->insert($data);
			}
			else
			{
				$this->model_addon_bds->update($data);
			}
			$this->data['output'] = "true";
			$this->model_core_file->clearTemp();
		}
		else
		{
			foreach($this->error as $item)
			{
				$this->data['output'] .= $item."<br>";
			}
		}
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	public function movetop()
	{
		$bdsid = $this->request->get['bdsid'];
		$this->load->model("addon/bds");
		$data = $this->model_addon_bds->getItem($bdsid);
		$this->model_addon_bds->updateStatus($data);
		$this->data['output'] = "true";
		$this->id='content';
		$this->template='common/output.tpl';
		$this->render();
	}
	
	private function validateForm($data)
	{
		//chua kiem tra trung
		
    	/*if (trim($data['bdsid']) == "") 
		{
      		$this->error['bdsid'] = "Mã đơn vị tính không được rỗng";
    	}
		else
		{
			if($data['id'] == "")
			{
				$this->load->model("addon/bds");
				$where = " AND bdsid ='".$data['bdsid']."'" ;
				$item = $this->model_addon_bds->getList($where);
				if(count($item)>0)
					$this->error['bdsid'] = "Mã bất động sản đã được sử dụng";
			}
		}*/
		if (trim($data['tieude']) == "") 
		{
      		$this->error['tieude'] = "Tiêu đề không được rỗng";
    	}
		
		if (trim($data['nhucau']) == "") 
		{
      		$this->error['nhucau'] = "Bạn chưa chọn nhu cầu";
    	}
		
		if (trim($data['loaibds']) == "") 
		{
      		$this->error['loaibds'] = "Bạn chưa chọn loại bất động sản";
    	}
		
		if (trim($data['huong']) == "") 
		{
      		$this->error['huong'] = "Bạn chưa chọn hướng";
    	}
		
		if (trim($data['khuvuc']) == "") 
		{
      		$this->error['khuvuc'] = "Bạn chưa chọn khu vực";
    	}
		
		if (trim($data['diachi']) == "") 
		{
      		$this->error['diachi'] = "Bạn chưa nhập địa chỉ";
    	}
		
		
		
		
		
		if (count($this->error)==0) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
	}
	//Cac ham xu ly tren form
	
}
?>